<?php

namespace App\Console\Commands;

use JonnyW\PhantomJs\Client;
use App\Jobs\DigArticleBdccx;
use Carbon\Carbon;
use QL\QueryList;
use Illuminate\Support\Facades\Cache;
use Illuminate\Console\Command;

class DigBdccx extends Command
{
    protected $client;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dig:bdccx {col} {link}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'dig:bdccx 144 http://www.bdccx.com/yh/list_1_1442.html';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $link   = $this->argument('link');
        $col    = $this->argument('col');
        $data   = $this->check($link);
        if($data){
            $index  = $this->ithome($data);
            if($index){
                $collection = collect($index);
                $multiplied = $collection->map(function ($item) use ($col) {
                    DigArticleBdccx::dispatch($item,$col);
                });
                $all    = $multiplied->all();
                $this->info('dig article ok!');
            }else{
                $this->error('dig article result!');
            }
        }else{
            $this->error('dig article error!');
        }
    }

    function ithome($out){
        $out    = preg_replace("/<[ ]+/si","<",$out); //过滤<("<"号后面带空格)
        $out    = preg_replace("/<\!–.*?–>/si","",$out); //注释
		$out    = preg_replace("'<script[^>]*?>.*?</script>'si", '', $out);
		$out    = preg_replace("/<(\/?head.*?)>/si", "", $out); //过滤head标签
		$out    = preg_replace("/<(\/?meta.*?)>/si", "", $out); //过滤meta标签
        $out    = preg_replace("/<(\/?body.*?)>/si", "", $out); //过滤body标签
        $out    = preg_replace("/<(\/?link.*?)>/si", "", $out); //过滤link标签
        $out    = preg_replace("'<style[^>]*?>.*?</style>'si", '', $out);
        $out    = preg_replace("/<(title.*?)>(.*?)<(\/title.*?)>/si","",$out); //过滤title标签
        $out    = preg_replace("/<(\/?title.*?)>/si","",$out); //过滤title标签
    	$html   = QueryList::html($out)->find('#wrap .container .main .sec-panel .post-loop');
        $ql     = QueryList::html($html->html())->rules([
            //采集规则
            'link'  => ['.item-img a:eq(0)','href'],
            'title' => ['.item-img a:eq(0)','title'],
        ])->range('li')->query()->getData(function ($item){
        	$item['link']	= 'http://www.bdccx.com'.$item['link'];
            return $item;
        });
        $data   = $ql->all();
        $collection = collect($data);
        if(!$collection->isEmpty()){
            $multiplied = $collection->map(function ($item, $k) use ($collection) {
                $key    = 'dig_article_bdccx'.md5($item['link']);
                $value = Cache::get($key);
                if($value){
                    $collection->pull($k);
                }else{
                    $expiresAt = Carbon::now()->addMinutes(env('DIG_CACHE'));
                    Cache::put($key, $item, $expiresAt);
                    return $item;
                }
            });
            return $multiplied->all();;
        }else{
            return false;
        }
    }
    //抓取网址
    function check($url){
        
        $client = Client::getInstance();

        /** 
         * @see JonnyW\PhantomJs\Http\Request
         **/
        $request = $client->getMessageFactory()->createRequest($url, 'GET');
    
        /** 
         * @see JonnyW\PhantomJs\Http\Response 
         **/
        $response = $client->getMessageFactory()->createResponse();
    
        // Send the request
        $client->send($request, $response);
    
        if($response->getStatus() === 200) {
    
            // Dump the requested page content
            return $response->getContent();
        }
        
    }
}
